﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Validation;
using System.Data.Entity.Core.EntityClient;
using WinnerCMS.Common.Extension;
using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// Article 的摘要说明。
    /// </summary>
    public class ArticleDal : CoreDal
    {


        #region 添加文章
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Article model)
        {
            using (Entities db = new Entities())
            {
                try
                {
                    var categoryIds = model.CategoryIds.ToArrInt();
                    var categores = db.Categoryes.Where(x => categoryIds.Contains(x.Id));

                    foreach (var v in categores)
                    {
                        model.DeputyCategoryes.Add(v);
                    }


                    db.Articles.Add(model);

                    db.SaveChangesAsync().Wait();
                    return model.Id;
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    throw;
                }
            }
        }
        #endregion

        #region 修改文章
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Article model)
        {
            using (Entities db = new Entities())
            {
                var info = db.Articles.FirstOrDefault(x => x.Id == model.Id);
                info.DeputyCategoryes.Clear();

                db.Entry<Article>(info).CurrentValues.SetValues(model);

                var categoryIds = model.CategoryIds.ToArrInt();
                var categores = db.Categoryes.Where(x => categoryIds.Contains(x.Id));
                foreach (var v in categores)
                {
                    model.DeputyCategoryes.Add(v);
                }


                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 加载文章
        /// <summary>
        /// 加载文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public new Article GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Articles.SqlQuery("SELECT * FROM CMS_Articles A INNER JOIN CMS_Cores C ON A.Id=C.Id WHERE C.Id={0}", id).FirstOrDefault();
            }
        }
        #endregion

        #region 绑定简单内容
        /// <summary>
        /// 绑定简单内容
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Article> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                return db.Database.SqlQuery<Article>(string.Format(@"SELECT  *
FROM    CMS_Cores C
        INNER JOIN CMS_Articles A ON A.Id = C.Id
WHERE   a.Id IN (
        SELECT  id
        FROM    ( SELECT    id ,
                            ROW_NUMBER() OVER ( ORDER BY Id DESC ) AS rank
                  FROM      CMS_Articles
                ) AS t
        WHERE   t.rank BETWEEN {0} AND {1} )", pp.StartRecord, pp.EndRecord)).ToArray();
            }
        }
        /// <summary>
        /// 统计内容数量，配合“绑定简单内容”
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            using (Entities db = new Entities())
            {
                return db.Articles.Count(x => !x.IsDeleted && x.IsChecked);
            }
        }
        #endregion
    }
}